/*
 * Copyright (C) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * created on : 2012-10-19
 * author : yanguoyue
 */

#include "napi/native_api.h"
#include <sstream>

#include "mars/comm/autobuffer.h"
#include "mars/comm/xlogger/xlogger.h"
#include "mars/comm/napi/util/var_cache.h"
#include "mars/comm/napi/util/comm_function.h"
#include "mars/comm/thread/mutex.h"
#include "mars/comm/thread/lock.h"

#include "mars/app/app.h"

namespace mars {
namespace app {

std::string GetAppFilePath() {
	xverbose_function();
    VarCache* cache_instance = VarCache::Singleton();
	napi_env env = cache_instance->GetJSEnv();
	
	napi_value path_value = JS_CallStaticMethodByName(env, "com.ohos.mars.app.AppLogic", "getAppFilePath", 0);
	
	if (NULL == path_value) {
		xerror2(TSF"getAppFilePath error return null");
		return "";
	}
	std::string app_path = "";
	GetNAPIStringValue(env, path_value, app_path);
	return app_path;
}

AccountInfo GetAccountInfo() {
	xverbose_function();
    VarCache* cache_instance = VarCache::Singleton();
    napi_env env = cache_instance->GetJSEnv();

    AccountInfo info;

    napi_value ret_obj = JS_CallStaticMethodByName(env, "com.ohos.mars.app.AppLogic", "getAccountInfo", 0);	
    if (NULL == ret_obj) {
    	xerror2(TSF"getAccountInfo error return null");
    	return info;
    }
    int64_t uin;
    JS_GetProperty(env, ret_obj, "uin", uin);
    std::string username_jstr;
    JS_GetProperty(env, ret_obj, "userName", uin);


    return info;
}

std::string GetUserName() {
    xverbose_function();
    VarCache* cache_instance = VarCache::Singleton();
    napi_env env = cache_instance->GetJSEnv();

    napi_value ret_obj = JS_CallStaticMethodByName(env, "com.ohos.mars.app.AppLogic", "getAccountInfo", 0);		
    if (NULL == ret_obj) {
    	return std::string();
    }

    std::string username;
    JS_GetProperty(env, ret_obj, "userName", username);

    return username;
}

std::string GetRecentUserName() {
	return GetUserName();
}

unsigned int GetClientVersion() {
    xverbose_function();
	static int s_version = 0;

	if (0 != s_version) {
		return s_version;
	}

	VarCache* cache_instance = VarCache::Singleton();
	napi_env env = cache_instance->GetJSEnv();

	napi_value version_value = JS_CallStaticMethodByName(env, "com.ohos.mars.app.AppLogic", "getClientVersion", 0);

	if (NULL == version_value) {
 		return 0;
 	}
	 
	napi_status status = napi_get_value_int32(env, version_value, &s_version);
	if (status != napi_ok) {
		s_version = 0;
	}

	return s_version;
}

DeviceInfo GetDeviceInfo() {
	xverbose_function();
	static DeviceInfo s_info = {"devicename0", "phone"};
	if (!s_info.devicename.empty() || !s_info.devicetype.empty()) {
		return s_info;
	}

	VarCache* cache_instance = VarCache::Singleton();
	napi_env env = cache_instance->GetJSEnv();

	napi_value ret_obj = JS_CallStaticMethodByName(env, "com.ohos.mars.app.AppLogic", "getDeviceType", 0);		 	
	if (NULL == ret_obj) {
 		xerror2(TSF"GetDeviceInfo error return null");
		return s_info;
 	}

	std::string devicename_jstr;

	JS_GetProperty(env, ret_obj, "devicename", devicename_jstr);
	
	static comm::Mutex mutex;
	comm::ScopedLock lock(mutex);

	s_info.devicename = devicename_jstr;

	std::string devicetype_jstr;
	JS_GetProperty(env, ret_obj, "devicetype", devicetype_jstr);

	s_info.devicetype = devicetype_jstr;

	return s_info;
}

mars::comm::ProxyInfo GetProxyInfo(const std::string& _host) {
    return mars::comm::ProxyInfo();
}

}
}
